package com.ostrich.a2104shopping.ui.home.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ostrich.a2104shopping.R;
import com.ostrich.a2104shopping.databinding.ItemCarBinding;
import com.ostrich.a2104shopping.databinding.ItemHomeGoodsBinding;
import com.ostrich.a2104shopping.ui.home.entity.CarEntity;
import com.ostrich.a2104shopping.ui.home.entity.GoodsEntity;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @Author : yaotianxue
 * @Time : On 2023/10/7 10:12
 * @Description : CarAdapter
 */
public class CarAdapter extends RecyclerView.Adapter<CarAdapter.CarViewHolder> {

    private List<CarEntity> mCarEntities = new ArrayList<>();

    //根据下标获得数据
    public CarEntity getDataByPosition(int position){
        if(position >= 0 && position <= mCarEntities.size()-1){
            return mCarEntities.get(position);
        }else{
            return null;
        }
    }
    //获得数据
    public List<CarEntity> getCarEntities() {
        return mCarEntities;
    }



    //追加数据
    public void setData(List<CarEntity> list){
        if(list == null || list.size() == 0){
            return;
        }
        mCarEntities.addAll(list);
        notifyDataSetChanged();
    }

    //刷新数据
    public void refreshData(List<CarEntity> list){
        if(list == null || list.size() == 0){
            return;
        }
        mCarEntities.clear();
        mCarEntities.addAll(list);
        notifyDataSetChanged();
    }

    //填充一条数据
    public void addData(CarEntity category){
        mCarEntities.add(category);
        notifyItemInserted(mCarEntities.size()-1);
    }
    //删除数据
    public void deleteData(int position){
        mCarEntities.remove(position);
        notifyItemRemoved(position);
    }
    //删除选中数据
    public void deleteCheck(){
        for (int i =  mCarEntities.size()-1; i > -1; i--) {
            if(mCarEntities.get(i).isCheck()){
               deleteData(i);
            }
        }
    }



    @NonNull
    @Override
    public CarViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemCarBinding itemCarBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_car, parent, false);
        return new CarViewHolder(itemCarBinding);
    }
    //局部增量刷新：用于只刷新一个条目上的一个小控件
    @Override
    public void onBindViewHolder(@NonNull CarViewHolder holder, int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        for (Object payload : payloads) {
            if(payload.equals("num")){
                holder.mItemCarBinding.tvNum.setText(mCarEntities.get(position).getCount()+"");
            }
        }
    }

    @Override
    public void onBindViewHolder(@NonNull CarViewHolder holder, int position) {
        holder.mItemCarBinding.setCar(mCarEntities.get(position));
        holder.mItemCarBinding.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mItemChildClickListener != null){
                    mItemChildClickListener.onItemChildClick(v,holder.getAdapterPosition());
                }
            }
        });
        holder.mItemCarBinding.ivStub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mItemChildClickListener != null){
                    mItemChildClickListener.onItemChildClick(v,holder.getAdapterPosition());
                }
            }
        });
        holder.mItemCarBinding.ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mItemChildClickListener != null){
                    mItemChildClickListener.onItemChildClick(v,holder.getAdapterPosition());
                }
            }
        });
        holder.mItemCarBinding.ivRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mItemChildClickListener != null){
                    mItemChildClickListener.onItemChildClick(v,holder.getAdapterPosition());
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mCarEntities.size();
    }

    class CarViewHolder extends RecyclerView.ViewHolder{
        ItemCarBinding mItemCarBinding;

        public CarViewHolder(ItemCarBinding itemCarBinding) {
            super(itemCarBinding.getRoot());
            this.mItemCarBinding = itemCarBinding;
        }
    }

    public void setItemChildClickListener(ItemChildClickListener itemChildClickListener) {
        mItemChildClickListener = itemChildClickListener;
    }

    private ItemChildClickListener mItemChildClickListener;
    public interface ItemChildClickListener{
        void onItemChildClick(View v,int position);
    }
}
